[Unit]
Description=IIAB ap0 clone wifi device
Wants=network-pre.target
# https://unix.stackexchange.com/questions/257888/systemd-wait-for-network-interface-to-be-up-before-running-service/417839#417839
# 2022-08-22: #3352 Raspberry Pi Zero W requires 2 lines below...
BindsTo=sys-subsystem-net-devices-{{ discovered_wireless_iface }}.device
After=sys-subsystem-net-devices-{{ discovered_wireless_iface }}.device
After=network-pre.target
After=systemd-rfkill.service
Before=dhcpcd.service
Before=wpa_supplicant.service
Before=wpa_supplicant@{{ discovered_wireless_iface }}.service
Before=NetworkManager.service
Before=netplan-wpa@{{ discovered_wireless_iface }}.service
Before=hostapd.service

[Service]
Type=oneshot
RemainAfterExit=yes
# 2025-03-12: #3961 phy0 can no longer be assumed (phy1 also occurs very regularly) -- so read phyname dynamically from /sys/class/ieee80211/ -- explanation at: roles/firmware/tasks/warn_fw_crash.yml
ExecStartPre=-/bin/bash -c '/sbin/iw phy "$(ls /sys/class/ieee80211/ | head -1)" interface add ap0 type __ap'
ExecStartPre=-/sbin/ip link set ap0 address {{ ap0_mac_addr }}
ExecStartPre=-/usr/sbin/rfkill unblock wlan
ExecStart=-/sbin/ip link set ap0 up
ExecStop=-/sbin/iw dev ap0 del

[Install]
WantedBy=multi-user.target
